package site.assad.i18n;



import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
import java.text.NumberFormat;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/12/1 12:18
 * Description: 测试 Locale，NumberFormat ,DateFormat, MessageFormat 类
 */
public class BasicTest {
    public static void main(String[] args){

        /*test Locale*/
        //带有语言和国际地区信息的 Locale
        Locale local1 = new Locale("zh","CN");
        //带有语言信息的 Locale
        Locale local2 = new Locale("zh");
        //等同于 Locale("zh","CN");
        Locale locale3 = Locale.CHINA;
        //等同于 Locale("zh");
        Locale locale4 = Locale.CHINESE;

        /*test NumberFormat*/
        Locale locale5 = new Locale("zh","CN");
        NumberFormat numberFmt = NumberFormat.getCurrencyInstance(locale5);
        double amt = 2333.33;
        String fmtStr = numberFmt.format(amt);
        System.out.println(fmtStr);   //output: ￥2,333.33



        /*test DateFormat*/
        Locale locale6 = new Locale("en","US");
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM,locale6);
        Date date = new Date();
        String fmtStr2 = dateFormat.format(date);
        System.out.println(fmtStr2);   //output:  Dec 1, 2017


        /*test MessageFormat*/
        //格式化字符串
        String pattern1 = "{0}，您于 {1,date,short} 支付 {2,number,currency} ";
        String pattern2 = "{0} ,you paid {2,number,currency} at {1,date,short} ";

        //动态替换格式化字符串的占位符的参数
        Object[] params = {"Al-assad",new Date(),1.0E3};

        //使用指定本地化对象格式化信息
        MessageFormat  msgFmt1 = new MessageFormat(pattern1,Locale.CHINA);
        MessageFormat msgFmt2 = new MessageFormat(pattern2,Locale.US);
        String msg1 = msgFmt1.format(params);   //"Al-assad，您于 17-12-1 支付 ￥1,000.00 "
        String msg2 = msgFmt2.format(params);   //"Al-assad ,you paid $1,000.00 at 12/1/17 "

        System.out.println(msg1+"\n"+msg2);

    }

}
